class Book:
    def __init__(self, id, title, author):
        self.id = id
        self.title = title
        self.author = author
        self.next = None

    def __repr__(self):
        return f"ID: {self.id}, Title: {self.title}, Author: {self.author}"


class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, book):
        if not self.head:
            self.head = book
        else:
            current = self.head
            while current.next:
                current = current.next
            current.next = book

    def delete(self, book_id):
        current = self.head
        prev = None
        while current and current.id != book_id:
            prev = current
            current = current.next
        if current is None:
            return None
        if prev is None:
            self.head = current.next
        else:
            prev.next = current.next
        return current

    def find(self, book_id):
        current = self.head
        while current and current.id != book_id:
            current = current.next
        return current

    def update(self, book_id, new_title, new_author):
        book = self.find(book_id)
        if book:
            book.title = new_title
            book.author = new_author
            return True
        return False

    def __repr__(self):
        books = []
        current = self.head
        while current:
            books.append(repr(current))
            current = current.next
        return '[' + ', '.join(books) + ']'


class HashTable:
    def __init__(self, size):
        self.size = size
        self.table = [LinkedList() for _ in range(size)]

    def get_hash(self, book_id):
        return book_id % self.size

    def insert(self, book):
        hash_index = self.get_hash(book.id)
        self.table[hash_index].append(book)

    def remove(self, book_id):
        hash_index = self.get_hash(book_id)
        return self.table[hash_index].delete(book_id)

    def search(self, book_id):
        hash_index = self.get_hash(book_id)
        return self.table[hash_index].find(book_id)

    def update(self, book_id, new_title, new_author):
        hash_index = self.get_hash(book_id)
        return self.table[hash_index].update(book_id, new_title, new_author)

    def __repr__(self):
        return '\n'.join(f"{idx}: {llist}" for idx, llist in enumerate(self.table))
def display_menu():
    print("\n欢迎进入图书管理系统")
    print("1. 添加一本图书")
    print("2. 删除一本图书")
    print("3. 需要为你查找哪本书")
    print("4. 更新一本图书")
    print("5. 显示所有的书籍")
    print("6. 退出图书管理系统")


def main():
    book_management = HashTable(10)  # 假设我们有10个槽位的哈希表

    while True:
        display_menu()
        choice = input("输入你的选择: ")

        if choice == '1':
            book_id = int(input("输入要添加书籍的编号: "))
            title = input("输入书名: ")
            author = input("输入书的作者: ")
            book_management.insert(Book(book_id, title, author))
            print("图书添加成功！")

        elif choice == '2':
            book_id = int(input("输入要删除书籍的编号: "))
            removed_book = book_management.remove(book_id)
            if removed_book:
                print("图书删除成功！.")
            else:
                print("没有找到该书.")

        elif choice == '3':
            book_id = int(input("输入要查找书籍的编号: "))
            book = book_management.search(book_id)
            if book:
                print("找到了:", book)
            else:
                print("找不到这本书.")

        elif choice == '4':
            book_id = int(input("输入要更新书籍的编号: "))
            new_title = input("输入新书的书名: ")
            new_author = input("输入新书的作者名: ")
            updated = book_management.update(book_id, new_title, new_author)
            if updated:
                print("图书更新成功.")
            else:
                print("找不到这本书.")

        elif choice == '5':
            print("所有的书籍如下:")
            print(book_management)

        elif choice == '6':
            print("退出图书管理系统.")
            break

        else:
            print("选项输入错误，请重新输入")


if __name__ == "__main__":
    main()
